package ivan.bookKeeper.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import domain.IGeneralDomainObject;

public class DatabaseHandler extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "LibraryCard.db";
	private static final int SCHEMA_VERSION = 1;
	private SQLiteDatabase db;
	private static final String TAG = DatabaseHandler.class.getSimpleName();

	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, SCHEMA_VERSION);
		if (db == null) {
			db = getWritableDatabase();
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		// this.db = db;
		String query = "CREATE TABLE TCity (cityID INTEGER PRIMARY KEY AUTOINCREMENT, cityName TEXT);";
		db.execSQL(query);
		Log.d(TAG, "--- CREATED TABLE query: " + query);

		query = "CREATE TABLE TLibrary (libraryID INTEGER PRIMARY KEY AUTOINCREMENT, borrowLength INTEGER, "
				+ "maxNumExtending INTEGER, libraryName TEXT, libraryAddress TEXT, libraryPhone TEXT, "
				+ "libraryEmail TEXT);";// , FOREIGN KEY(cityID) REFERENCES
										// TCity(cityID) NOT NULL );";
		db.execSQL(query);
		Log.d(TAG, "--- CREATED TABLE query: " + query);

		query = "CREATE TABLE TBook (bookID INTEGER PRIMARY KEY AUTOINCREMENT, bookYear INTEGER, bookName TEXT, "
				+ "bookAuthor TEXT, bookPublisher TEXT, bookLanguage TEXT, bookISBN TEXT);";
		db.execSQL(query);
		Log.d(TAG, "--- CREATED TABLE query: " + query);

		query = "CREATE TABLE TBookFromLibrary (libraryID REFERENCES TLibrary(libraryID), "
				+ "bookID REFERENCES TBook(bookID), numExtend INTEGER, date LONG NOT NULL DEFAULT (strftime('%s','now')), PRIMARY KEY(libraryID,"
				+ "bookID));";
		db.execSQL(query);
		Log.d(TAG, "--- CREATED TABLE query: " + query);

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int oldVersion, int newVersion) {

	}

	public void insertObject(IGeneralDomainObject gdo) {
		String sql = "INSERT INTO " + gdo.getTableName() + " VALUES("
				+ gdo.getInsertValues() + ")";
		Log.d(TAG, "---------- Prepared sql statement: " + sql);
		db.execSQL(sql);
		Log.d(TAG, "---------- Executed sql statement: " + sql);
	}

	public Cursor fetchAll(IGeneralDomainObject gdo) {
		Cursor cursor = db.query(gdo.getSimpleTableName(),
				gdo.getColumnsNames(), null, null, null, null, null);
		Log.d(TAG,
				"Executed query : GOT ALL ROWS FROM "
						+ gdo.getSimpleTableName());
		return cursor;
	}

	public Cursor fetch(IGeneralDomainObject gdo) {
		Cursor cursor = db.query(gdo.getSimpleTableName(),
				gdo.getColumnsNames(), gdo.getFindValue(), null, null, null,
				null);
		Log.d(TAG, "Executed query: FIND ROWS WITH QUERY");
		return cursor;
	}

	public void updateObject(IGeneralDomainObject gdo) {
		String sql = "UPDATE " + gdo.getSimpleTableName() + " SET "
				+ gdo.getUpdateValue() + " WHERE " + gdo.getUniqueCondition();
		Log.d(TAG, "------- Prepared sql statement: " + sql);
		db.execSQL(sql);
		Log.d(TAG, "-------- Executed sql statement: " + sql);
	}

	public void deleteObject(IGeneralDomainObject gdo) {
		String sql = "DELETE " + gdo.getDeleteValue() + " FROM "
				+ gdo.getSimpleTableName() + " WHERE "
				+ gdo.getDeleteCondition();
		Log.d(TAG, "------- Prepared sql statement: " + sql);
		db.execSQL(sql);
		Log.d(TAG, "-------- Executed sql statement: " + sql);
	}
}
